/**
 * Implementation of ITOA with 0-filling.
 *
 * @author Timothy Flynn (trflynn@wpi.edu)
 * @modified by JTM 2012/04/11
 *
 * @version April 11, 2012
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "common.h"
#include "PhysicalLayer.h"
#include "DataLinkLayer.h"
#include "ApplicationLayer.h"

/**
 * ITOA with 0-filling. Converts number to a string of length sz. All
 * prefix spots are filled with '0'.
 */
char *stoa(short n, int sz) {
	char *s = (char*)malloc(sz * sizeof(char));

	sprintf(s, "%d", n);
	int len = strlen(s);

	for(int i=len; i<sz; i++) {
		s[i] = 0x30;
	}

	for(int i=0; i<(sz-len); i++) {
		for(int j=sz-1; j>0; j--) {
			s[j] = s[j-1];
		}
	}

	for(int i=0; i<(sz-len); i++) {
		s[i]= 0x30;
	}

	return s;
}

char *ustoa(unsigned short n, int sz) {
	char *s = (char*)malloc(sz * sizeof(char));

	sprintf(s, "%u", n);
	int len = strlen(s);

	for(int i=len; i<sz; i++) {
		s[i] = 0x30;
	}

	for(int i=0; i<(sz-len); i++) {
		for(int j=sz-1; j>0; j--) {
			s[j] = s[j-1];
		}
	}

	for(int i=0; i<(sz-len); i++) {
		s[i]= 0x30;
	}

	return s;
}

/**
 * Function to run physical layer.
 */
void *runPhysicalLayer(void *p) {
        ((PhysicalLayer *)p)->run();
        return (void *)0;
}

/**
 * Function to run data link layer.
 */
void *runDataLinkLayer(void *p) {
        ((DataLinkLayer *)p)->goBackN();
        return (void *)0;
}
